const app = getApp()
Page({
  data: {
    upurl: app.globalData.upurl,
    imgurl: app.globalData.imgurl,
    star: 1,
    fileList: [],
    remark: '',
  },
  onLoad({ o_id }) {
    this._getData(o_id)
    this.setData({ o_id })
  },
  async submit() {
    const { star, fileList, remark, o_id } = this.data
    let pics = fileList.map((item) => {
      return item.url
    })
    let { code } = await wx.requestAsync({
      url: '/order/evaluate',
      data: {
        o_id,
        star,
        remark,
        pics: pics.join(),
      },
    })
    if (code == 0) {
      wx.showToast({
        title: '评价成功',
        icon: 'none',
        duration: 2000,
        success: () => {
          setTimeout(() => {
            wx.redirectTo({
              url: '/pages/appointment/appointment',
            })
          }, 1000)
        },
      })
    }
  },
  onChangeStar({ detail }) {
    this.setData({
      star: detail,
    })
  },
  onChangeRemark({ detail }) {
    console.log(detail)

    this.setData({
      remark: detail,
    })
  },
  async _getData(o_id) {
    let { code, data } = await wx.requestAsync({
      url: '/order/detail',
      data: {
        o_id,
      },
    })
    if (code == 0) {
      this.setData(data)
    }
  },
  // 去往门店
  _toStore({
    currentTarget: {
      dataset: { id },
    },
  }) {
    wx.navigateTo({
      url: '/pages/storeIntroduction/storeIntroduction?s_id=' + id,
    })
  },
  _del({ detail: { index } }) {
    let { fileList } = this.data
    fileList.splice(index, 1)
    this.setData({
      fileList,
    })
  },
  afterRead({ detail: { file } }) {
    const { upurl, imgurl } = this.data
    const _this = this
    file.map((item) => {
      wx.uploadFile({
        url: upurl,
        filePath: item.path,
        name: 'file',
        header: {
          'auth-token': wx.getStorageSync('token') || '',
        },
        success({ data }) {
          let {
            data: { save_name },
          } = JSON.parse(data)
          const { fileList = [] } = _this.data
          fileList.push({ url: imgurl + save_name, isImage: true })
          _this.setData({ fileList })
        },
      })
    })
  },
})
